package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.entity.Friends;
import com.zy.entity.JsonResult;
import com.zy.entity.User;
import com.zy.mapper.FriendsMapper;
import com.zy.mapper.UserMapper;
import com.zy.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/app")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private FriendsMapper friendMapper;

    /**
     * 根据id查询好友列表
     *
     * @param uid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-06 18:18
     */
    @GetMapping("/getFriendsList")
    @ResponseBody
    public JsonResult getFriendsList(Integer uid) {
        List<User> userList = userMapper.userList(uid);
        Map<String, List<User>> groups = userList.stream().collect(Collectors.groupingBy(User::getLetter, TreeMap::new, Collectors.toList()));
        return new JsonResult("200", "", groups);
    }

    /**
     * 根据id查询好友信息
     *
     * @param id
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-06 18:18
     */
    @GetMapping("/getFriendsInfo")
    @ResponseBody
    public JsonResult getFriendsInfo(Integer id) {
        User user = userService.getById(id);
        user.setOpenId(null);
        return new JsonResult("200", "", user);
    }

    /**
     * 根据手机号模糊查询好友信息
     *
     * @param phone
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-06 18:18
     */
    @GetMapping("/getFriends")
    @ResponseBody
    public JsonResult getFriends(Integer id, String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.likeRight("phone", phone);
        queryWrapper.ne("id", id);
        queryWrapper.select("id", "name", "phone", "path", "post", "email", "birthday");
        List<User> userList = userService.list(queryWrapper);
        return new JsonResult("200", "", userList);
    }


    /**
     * 判断是否为好友
     *
     * @param uid
     * @param fid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-08 20:30
     */
    @GetMapping("/getFriendsByFid")
    @ResponseBody
    public JsonResult getFriendsByFid(Integer uid, Integer fid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("fid", fid);
        Friends friends = friendMapper.selectOne(queryWrapper);
        if (friends == null) {
            return new JsonResult("200", "", "0");
        }
        return new JsonResult("201", "", "1");
    }

    /**
     * 用户添加好友
     *
     * @param uid
     * @param fid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-07 14:06
     */
    @GetMapping("/addFriends")
    @ResponseBody
    public JsonResult addFriends(Integer uid, Integer fid) {
        Friends friends = new Friends();
        friends.setUid(uid);
        friends.setFid(fid);
        friendMapper.insert(friends);

        List<User> userList = userMapper.userList(uid);
        Map<String, List<User>> groups = userList.stream().collect(Collectors.groupingBy(User::getLetter, Collectors.toList()));
        return new JsonResult("200", "添加成功", groups);
    }

    /**
     * 删除好友
     *
     * @param uid
     * @param fid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-15 14:28
     */
    @GetMapping("/delFriends")
    @ResponseBody
    public JsonResult delFriends(Integer uid, Integer fid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("fid", fid);
        friendMapper.delete(queryWrapper);

        List<User> userList = userMapper.userList(uid);
        Map<String, List<User>> groups = userList.stream().collect(Collectors.groupingBy(User::getLetter, Collectors.toList()));
        return new JsonResult("200", "成功", groups);
    }

    /**
     * 查询用户 职位部门
     *
     * @param uid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-15 14:31
     */
    @GetMapping("/getDepartmentName")
    @ResponseBody
    public JsonResult getDepartmentName(Integer uid) {
        Map<String, String> departmentMap = userMapper.getDepartmentName(uid);
        return new JsonResult("200", "成功", departmentMap);
    }

}
